import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:yurun_smartmerchants/models/car_add_entity.dart';
import 'package:yurun_smartmerchants/models/custom_list_entity.dart';
import 'package:yurun_smartmerchants/net/ApiInterface.dart';
import 'package:yurun_smartmerchants/net/NetUtil.dart';
import 'package:yurun_smartmerchants/res/colours.dart';
import 'package:yurun_smartmerchants/utils/app_size.dart';
import 'package:yurun_smartmerchants/utils/evenbus_utils.dart';
import 'package:yurun_smartmerchants/utils/loading_utils.dart';

class CustomAdd extends StatefulWidget {
  final CustomListContent customListContent;

  CustomAdd({Key key, this.customListContent});

  @override
  _CustomAddState createState() => _CustomAddState();
}

class _CustomAddState extends State<CustomAdd> {
  var bus = EventBus();
  final nameController = TextEditingController();
  final phoneController = TextEditingController();

  @override
  void initState() {
    super.initState();

  }

  @override
  void dispose() {
    super.dispose();
    nameController.dispose();
    phoneController.dispose();
  }

  void add() {
    LoadingUtil.showLoading(context, tip: '加载中...');
    ApiInterface.getCustomAdd(nameController.text, phoneController.text)
        .then((value) {
      CarAddEntity carAddEntity = CarAddEntity.fromJson(value);
      if (carAddEntity.success) {
        Navigator.pop(context);
        bus.send("addCustom", "");
      }
    }).whenComplete(() => LoadingUtil.hideLoading(context));
  }

  void remove() {
    LoadingUtil.showLoading(context, tip: '加载中...');
    ApiInterface.getCustomdelete(widget.customListContent.id).then((value) {
      CarAddEntity carAddEntity = CarAddEntity.fromJson(value);
      if (carAddEntity.success) {
        Navigator.pop(context);
        bus.send("addCustom", "");
      }
    }).whenComplete(() => LoadingUtil.hideLoading(context));
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colours.white,
          centerTitle: true,
          iconTheme: IconThemeData(color: Colours.app_words),
          title: Text(
            widget.customListContent == null ? '新增客户' : "客户详情",
            style: TextStyle(fontSize: 18, color: Colours.app_words),
          )),
      body: Container(
        color: Colours.app_background,
        child: Column(
          children: <Widget>[
            Container(
              color: Colours.white,
              margin: EdgeInsets.only(top: AppSize.height(20)),
              height: AppSize.height(98),
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: Container(
                        margin: EdgeInsets.only(left: AppSize.width(32)),
                        child: Text(
                          '客户姓名',
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: Container(
                        alignment: Alignment.centerRight,
                        margin: EdgeInsets.only(right: AppSize.width(32)),
                        child: widget.customListContent == null
                            ? TextField(
                                controller: nameController,
                                textAlign: TextAlign.right,
                                style: TextStyle(
                                    color: Colours.color_313131,
                                    fontSize: AppSize.sp(32)),
                                decoration: InputDecoration(
                                    hintText: "输入客户姓名",
                                    border: InputBorder.none,
                                    counterText: '',
                                    hintStyle: TextStyle(
                                        fontSize: AppSize.sp(32),
                                        color: Colours.color_abacb5)),
                                onChanged: (value) {},
                                maxLength: 11,
                              )
                            : Text(
                                widget.customListContent.name,
                                style: TextStyle(
                                    fontSize: AppSize.sp(32),
                                    color: Colours.color_313131),
                              ),
                      )),
                ],
              ),
            ),
            Divider(
              thickness: AppSize.height(1),
              height: AppSize.height(1),
              color: Colours.app_background,
            ),
            Container(
              color: Colours.white,
              height: AppSize.height(98),
              child: Row(
                children: <Widget>[
                  Expanded(
                      flex: 1,
                      child: Container(
                        margin: EdgeInsets.only(left: AppSize.width(32)),
                        child: Text(
                          '联系电话',
                          style: TextStyle(
                              fontSize: AppSize.sp(32),
                              color: Colours.color_313131),
                        ),
                      )),
                  Expanded(
                      flex: 1,
                      child: Container(
                        alignment: Alignment.centerRight,
                        margin: EdgeInsets.only(right: AppSize.width(32)),
                        child: widget.customListContent == null
                            ? TextField(
                                controller: phoneController,
                                textAlign: TextAlign.right,
                                style: TextStyle(
                                    color: Colours.color_313131,
                                    fontSize: AppSize.sp(32)),
                                decoration: InputDecoration(
                                    hintText: "输入联系电话",
                                    border: InputBorder.none,
                                    counterText: '',
                                    hintStyle: TextStyle(
                                        fontSize: AppSize.sp(32),
                                        color: Colours.color_abacb5)),
                                onChanged: (value) {},
                                keyboardType: TextInputType.number,
                                maxLength: 11,
                              )
                            : Text(
                                widget.customListContent.phone,
                                style: TextStyle(
                                    fontSize: AppSize.sp(32),
                                    color: Colours.color_313131),
                              ),
                      )),
                ],
              ),
            ),
            Container(
              width: double.infinity,
              margin: EdgeInsets.only(
                  top: AppSize.height(80),
                  left: AppSize.width(32),
                  right: AppSize.width(32)),
              height: AppSize.height(88),
              child: RaisedButton(
                onPressed: () {
                  if (widget.customListContent == null) {
                    if (nameController.text == null ||
                        nameController.text.isEmpty) {
                      showToast("客户姓名不能为空");
                      return;
                    }
                    if (phoneController.text == null ||
                        phoneController.text.isEmpty) {
                      showToast("客户电话不能为空");
                      return;
                    }
                    if (phoneController.text.length != 11) {
                      showToast("客户电话格式不正确");
                      return;
                    }
                    add();
                  } else {
                    remove();
                  }
                },
                color: widget.customListContent == null
                    ? Colours.app_main
                    : Colours.white,
                child: Text(
                  widget.customListContent == null ? "新增客户" : "删除客户",
                  style: widget.customListContent == null
                      ? TextStyle(
                          fontSize: AppSize.sp(32), color: Colours.white)
                      : TextStyle(
                          fontSize: AppSize.sp(32),
                          color: Colours.color_313131),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
